//
//  HomeChooseAlertView.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/6.
//

#import "HomeChooseAlertView.h"
#import "HomeSearchAlertTableViewCell.h"
#import "HomeTransforOrderNetWorking.h"

@interface HomeChooseAlertView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>


@property (nonatomic,strong) UIView * follewView;
@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UIButton * canleBtn;

@property (nonatomic,strong) UITextField * searchTextF;
@property (nonatomic,strong) UILabel * resultLabel;
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;

@property (nonatomic,strong) HomeTransforOrderSimpleNickNameNetWorking * manager;


@end


@implementation HomeChooseAlertView


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self){
        
        [self setUpUI];
        
    }
    return  self;
}





-(void)setUpUI{
    
    [self addSubview:self.follewView];
    [self.follewView addSubview:self.titleLabel];
    [self.follewView addSubview:self.canleBtn];
    [self.follewView addSubview:self.searchTextF];
    [self.follewView addSubview:self.resultLabel];
    [self.follewView addSubview:self.tableView];
    
    [self.follewView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.follewView);
        make.top.equalTo(self.follewView).with.offset(ALIGN_HEIGHT_PT(30));
    }];
    
    [self.canleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(15));
        make.top.equalTo(self.follewView).with.offset(ALIGN_HEIGHT_PT(15));
        make.size.equalTo(@(CGSizeMake(ALIGN_WIDTH_PT(22), ALIGN_WIDTH_PT(22))));
    }];
    
    [self.searchTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.follewView).with.offset(ALIGN_WIDTH_PT(15));
        make.right.equalTo(self.follewView).with.offset(-ALIGN_WIDTH_PT(15));
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(ALIGN_HEIGHT_PT(20));
        make.height.equalTo(@(ALIGN_HEIGHT_PT(35)));
    }];
    
    [self.resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchTextF.mas_left);
        make.top.equalTo(self.searchTextF.mas_bottom).with.offset(ALIGN_HEIGHT_PT(12.5));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchTextF.mas_left);
        make.right.equalTo(self.searchTextF.mas_right);
        make.top.equalTo(self.resultLabel.mas_bottom);
        make.bottom.equalTo(self.follewView);
    }];
    
}

-(void)setFormInt:(NSInteger)formInt{
    _formInt = formInt;
}

#pragma mark -- 点击事件
-(void)respondsTo_canleBtn{

    if (self.delegate && [self.delegate respondsToSelector:@selector(canleAlertView)]){
        [self.delegate canleAlertView];
    }
    
}



#pragma mark -- UITextFieldDelegate

-(void)textFieldDidEndEditing:(UITextField *)textField{
    [self textFieldShouldReturn:textField];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    if ([textField.text length] < 2){
        return NO;
    }else{
        
        NSDictionary * dic = @{@"nickname":textField.text,
                               @"userId":[defaults valueForKey:@"userId"]
        };
        [self.manager dispatchTaskWithParam:dic completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (responseObject && [responseObject[@"code"] intValue] == 200){
                
                self.dataSource = [NSMutableArray arrayWithArray:responseObject[@"data"]];
                [self.tableView reloadData];
                
                
            }else{
                [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:nil];
                return;
            }
        }];
        
        
        
    }
    
    
    
    return YES;
}






#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeSearchAlertTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeSearchAlertTableViewCell"];
    if (!cell){
        cell = [[HomeSearchAlertTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeSearchAlertTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text = [NSString stringWithFormat:@"%@",self.dataSource[indexPath.row][@"nickname"]];
    cell.msgLabel.text = [NSString stringWithFormat:@"%@/%@",self.dataSource[indexPath.row][@"companyName"],self.dataSource[indexPath.row][@"orgName"]];

    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * nameStr = [NSString stringWithFormat:@"%@",self.dataSource[indexPath.row][@"nickname"]];
    NSString * str = [NSString stringWithFormat:@"%@",self.dataSource[indexPath.row][@"userId"]];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(choosePerson:UserId:formInt:)]){
        [self.delegate choosePerson:nameStr UserId:str formInt:self.formInt];
    }
}





-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(50);
}


#pragma mark -- 懒加载
-(UIView *)follewView{
    if (!_follewView){
        _follewView = [UIView new];
        _follewView.backgroundColor = [UIColor whiteColor];
        _follewView.layer.cornerRadius = 16;
        _follewView.layer.masksToBounds = YES;
    }
    return _follewView;
}

-(UILabel *)titleLabel{
    if (!_titleLabel){
        _titleLabel = [UILabel new];
        _titleLabel.text = @"选择接收/抄送人";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = FONT21;
        _titleLabel.textColor = [UIColor blackColor];
    }
    return _titleLabel;
}

-(UIButton *)canleBtn{
    if (!_canleBtn){
        _canleBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_canleBtn setImage:[UIImage imageNamed:@"canle"] forState:(UIControlStateNormal)];
        [_canleBtn addTarget:self action:@selector(respondsTo_canleBtn) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _canleBtn;
}

-(UITextField *)searchTextF{
    if (!_searchTextF){
        _searchTextF = [[UITextField alloc]init];
        _searchTextF.placeholder = @"请输入";
        _searchTextF.textColor = [UIColor blackColor];
        _searchTextF.font = FONT14;
        _searchTextF.backgroundColor = COLOR_RGB(242, 242, 242);
        _searchTextF.layer.cornerRadius = ALIGN_HEIGHT_PT(35) / 2;
        _searchTextF.layer.masksToBounds = YES;
        _searchTextF.textAlignment = NSTextAlignmentLeft;
        UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
        _searchTextF.leftView = leftView;
        _searchTextF.leftViewMode = UITextFieldViewModeAlways;
        _searchTextF.delegate = self;
    }
    return _searchTextF;
}

-(UILabel *)resultLabel{
    if (!_resultLabel){
        _resultLabel = [UILabel new];
        _resultLabel.textColor = [UIColor blackColor];
        _resultLabel.font = FONT14;
        _resultLabel.textAlignment = NSTextAlignmentLeft;
        _resultLabel.text = @"搜索结果";
    }
    return _resultLabel;
}

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(HomeTransforOrderSimpleNickNameNetWorking *)manager{
    if (!_manager){
        _manager = [HomeTransforOrderSimpleNickNameNetWorking new];
    }
    return _manager;
}

@end
